package com.xxg.renrenuser.controller;

import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrenuser.domain.query.CodeTableQuery;
import com.xxg.renrenuser.domain.query.UpdateCodeTableQuery;
import com.xxg.renrenuser.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 字典接口
 *
 * @author xxg
 * @date 2020/11/24
 */
@Api(tags = "字典管理")
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {

    private final DictionaryService dictionaryService;

    public DictionaryController(DictionaryService dictionaryService) {
        this.dictionaryService = dictionaryService;
    }

    @ApiOperation("获取所有代码表")
    @RequestMapping(value = "getAllCodeTables", method = RequestMethod.GET)
    public BaseResult getAllCodeTables() {
        return dictionaryService.getAllCodeTables();
    }

    @ApiOperation("获取所有代码表(键值对数组)")
    @RequestMapping(value = "getAllCodeTablesKv", method = RequestMethod.GET)
    public BaseResult getAllCodeTablesKv() {
        return dictionaryService.getallcodetableskv();
    }

    @ApiOperation("获取特定代码表")
    @RequestMapping(value = "getCodeTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "codeTableQuery", value = "代码表名称", required = true, dataType = "CodeTableQuery")})
    public BaseResult getCodeTable(@RequestBody @Validated CodeTableQuery codeTableQuery) {
        return dictionaryService.getCodeTable(codeTableQuery.getTableName());
    }

    @ApiOperation("更新特定代码表")
    @RequestMapping(value = "updateCodeTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "updateCodeTableQuery", value = "代码表信息", required = true, dataType = "UpdateCodeTableQuery")})
    public BaseResult updateCodeTable(@RequestBody @Validated UpdateCodeTableQuery updateCodeTableQuery) {
        return dictionaryService.updateCodeTable(updateCodeTableQuery.getTableName(), updateCodeTableQuery.getKeyValues());
    }
}
